/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.LicenseInfo;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.CooperBillItem;
import com.minierp.model.Establishment;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class CooperBillListForAccountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"CooperBillListForAccount.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFFont normalFont = workbook.createFont();
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle redDataCellStyle = workbook.createCellStyle();
		HSSFFont redFont = workbook.createFont();
		redFont.setColor(10);
		redDataCellStyle.setFont(redFont);
		redDataCellStyle.setBorderBottom(1);
		redDataCellStyle.setBorderTop(1);
		redDataCellStyle.setBorderLeft(1);
		redDataCellStyle.setBorderRight(1);
		HSSFCellStyle norDataCellStyle = workbook.createCellStyle();
		norDataCellStyle.setBorderBottom(1);
		norDataCellStyle.setBorderTop(1);
		norDataCellStyle.setBorderLeft(1);
		norDataCellStyle.setBorderRight(1);
		HSSFCellStyle boldCellStyle = workbook.createCellStyle();
		boldCellStyle.setFont(boldFont);
		boldCellStyle.setBorderBottom(1);
		boldCellStyle.setBorderTop(1);
		boldCellStyle.setBorderLeft(1);
		boldCellStyle.setBorderRight(1);
		HSSFCellStyle rightCellStyle = workbook.createCellStyle();
		rightCellStyle.setAlignment(3);
		Establishment est = (Establishment) model.get("est");
		BigDecimal favTotal = (BigDecimal) model.get("favTotal");
		BigDecimal costTotal = (BigDecimal) model.get("costTotal");
		HSSFSheet sheet = workbook.createSheet("list");
		LicenseInfo lif = EncCommonHelper.getLicenseInfo(request);
		byte colNum = 11;
		HSSFRow r = sheet.createRow(0);
		r.setHeight(600);
		HSSFCell c = r.createCell(0);

		for (int ts = 1; ts < colNum; ++ts) {
			r.createCell((short) ts);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(lif.getEnterpriseName()
				+ " 外协对帐单"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		HSSFRichTextString arg32 = new HSSFRichTextString("外协商名称："
				+ est.getEstName());
		arg32.applyFont(0, 6, boldFont);
		arg32.applyFont(6, arg32.length(), normalFont);
		c.setCellValue(arg32);
		sheet.addMergedRegion(new Region(1, 0, 1, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg32 = new HSSFRichTextString("起止日期："
				+ model.get("start_date").toString() + " 至 "
				+ model.get("end_date").toString());
		arg32.applyFont(0, 5, boldFont);
		arg32.applyFont(5, arg32.length(), normalFont);
		c.setCellValue(arg32);
		sheet.addMergedRegion(new Region(1, 3, 1, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		arg32 = new HSSFRichTextString("合同金额：" + model.get("costTotal"));
		arg32.applyFont(0, 5, boldFont);
		arg32.applyFont(5, arg32.length(), normalFont);
		c.setCellValue(arg32);
		sheet.addMergedRegion(new Region(1, 6, 1, 10));
		r = sheet.createRow(2);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		arg32 = new HSSFRichTextString("已付款金额：" + model.get("payTotal"));
		arg32.applyFont(0, 6, boldFont);
		arg32.applyFont(6, arg32.length(), normalFont);
		c.setCellValue(arg32);
		sheet.addMergedRegion(new Region(2, 0, 2, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg32 = new HSSFRichTextString("已调账金额：" + model.get("adjustTotal"));
		arg32.applyFont(0, 6, boldFont);
		arg32.applyFont(6, arg32.length(), normalFont);
		c.setCellValue(arg32);
		sheet.addMergedRegion(new Region(2, 3, 2, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		arg32 = new HSSFRichTextString("尚欠金额：" + model.get("remainTotal"));
		arg32.applyFont(0, 5, boldFont);
		arg32.applyFont(5, arg32.length(), normalFont);
		c.setCellValue(arg32);
		sheet.addMergedRegion(new Region(2, 6, 2, 10));
		r = sheet.createRow(3);
		c = r.createCell(0);

		for (int list = 1; list < colNum; ++list) {
			r.createCell((short) list);
		}

		c.setCellValue(new HSSFRichTextString(""));
		sheet.addMergedRegion(new Region(3, 0, 3, (short) (colNum - 1)));
		r = sheet.createRow(4);
		c = r.createCell(0);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(1);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(2);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("项目类型"));
		c = r.createCell(3);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("内容"));
		c = r.createCell(4);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("材质"));
		c = r.createCell(5);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("规格"));
		c = r.createCell(6);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(7);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(8);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(9);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(10);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("制作要求"));
		List arg33 = (List) model.get("list");
		List mtList = (List) model.get("mtList");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		int countStr;
		for (countStr = 0; countStr < arg33.size(); ++countStr) {
			Object[] os = (Object[]) arg33.get(countStr);
			CooperBillItem item = (CooperBillItem) os[0];
			String paied = (String) os[3];
			String spec = item.getSpecification();
			if (spec == null) {
				spec = "";
			}

			if (item.getPriceType().equals("AREA")
					|| item.getPriceType().equals("LENGTH")) {
				spec = spec + " x " + item.getAmount();
			}

			String unit = item.getUnit();
			if (item.getPriceType().equals("AREA")) {
				unit = "平方米";
			} else if (item.getPriceType().equals("LENGTH")) {
				if (item.getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}
			}

			BigDecimal amount = item.getSquareNum();
			if (item.getPriceType().equals("AMOUNT")) {
				amount = item.getAmount();
			}

			r = sheet.createRow(countStr + 5);
			c = r.createCell(0);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString((String) os[1]));
			c = r.createCell(1);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(sf.format((Date) os[2])));
			c = r.createCell(2);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(mtList, item.getMakeTypeId())));
			c = r.createCell(3);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getFinishedContent()));
			c = r.createCell(4);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getProName()));
			c = r.createCell(5);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(spec));
			c = r.createCell(6);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(unit));
			c = r.createCell(7);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(amount.doubleValue());
			c = r.createCell(8);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(item.getUnitPrice().doubleValue());
			c = r.createCell(9);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(item.getCost().doubleValue());
			c = r.createCell(10);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getMakeRequire()));
		}

		r = sheet.createRow(arg33.size() + 5);
		c = r.createCell(0);

		for (countStr = 1; countStr < colNum; ++countStr) {
			r.createCell((short) countStr);
		}

		c.setCellStyle(rightCellStyle);
		String arg34 = "";
		arg34 = "小计金额：" + favTotal.add(costTotal) + "元  扣除优惠金额：" + favTotal
				+ "元  ";
		arg34 = arg34 + "合同金额(实收)：" + costTotal + "元";
		c.setCellValue(new HSSFRichTextString(arg34));
		sheet.addMergedRegion(new Region(arg33.size() + 5, 0, arg33.size() + 5,
				(short) (colNum - 1)));
		sheet.setColumnWidth(0, 5000);
		sheet.setColumnWidth(1, 3200);
		ServletOutputStream arg35 = response.getOutputStream();
		workbook.write(arg35);
		arg35.flush();
		arg35.close();
	}
}